package com.xiongwei.thread;

/**
 * Java用Thread对象表示一个线程,通过调用start(注意不是run)方法启动一个线程.
 * 一个线程对象只能调用一次start方法
 * 线程的执行代码写在run方法中
 * 线程调度顺序由操作系统决定,程序本身无法决定
 * Thread.sleep可以吧当前线程暂定一段时间
 *
 * @author XiongWei
 * @date 2021/1/13
 */
public class CreateThread {
    public static void main(String[] args) {
        Thread t1 = new MyThread1();
        Thread t2 = new Thread(new MyThread2());
        Thread t3 = new Thread(() -> System.out.println("start my thread three, it is lambda expression"));
        t3.setPriority(1);
        t1.start();
        t2.start();
        t3.start();
    }
}

class MyThread1 extends Thread {
    @Override
    public void run() {
        //可以在线程中调用Thread.sleep(毫秒数)，强迫当前线程暂停一段时间：
        try {
            Thread.sleep(10);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("start my thread one, it is extends Thread");
    }
}

class MyThread2 implements Runnable {
    @Override
    public void run() {
        System.out.println("start my thread two, it is implement Runnable");
    }
}
